/**
 *	部门
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-28
 */

package com.framework.Model;



import com.framerock.db.DB;
import com.framerock.db.DBConn;
import com.framework.ACore.Model;
import com.framerock.baselib.Rock;

import java.util.ArrayList;
import java.util.Map;


public class ModelDept extends Model {

    public ModelDept(DBConn dbs) {
        super(dbs);
        this.setTable("dept");
    }

    public static ModelDept get(DBConn dbs)
    {
        return new ModelDept(dbs);
    }

    /**
     * 人员的路径
     * */
    public String useraPath(Map<String, String> ars)
    {
        String deptpath = this.getDeptpath(ars.get("deptid"), "d");
        deptpath    = "a"+ars.get("id")+","+deptpath+"";
        return deptpath;
    }

    /**
     * 获取上级部门路径
     * */
    private String deptpath = "";
    public String getDeptpath(String id, String lx)
    {
        deptpath = lx+id;
        getDeptpaths(id, lx);
        return deptpath;
    }
    private void getDeptpaths(String id, String lx){
        Map<String, String> rs = this.getXinxi(id);
        if(rs == null)return;
        int pid = Rock.getMapint(rs, "pid");
        if(pid>0){
            deptpath =""+lx+pid+","+deptpath+"";
            this.getDeptpaths(pid+"", lx);
        }
    }

    /**
     * 获取所有下级部门ID
     * */
    public String getDeptdown(String id)
    {
        deptpath = id;
        this.getDeptdowns(id);
        return deptpath;
    }
    private void getDeptdowns(String id){
        ArrayList<Map<String, String>> rows = this.getAll("`pid`="+id+"");
        String nid;
        if(rows !=null)for(int i=0;i<rows.size();i++){
            nid = rows.get(i).get("id");
            deptpath+=","+nid+"";
            this.getDeptdowns(nid);
        }
    }

    /**
     * 条件处理
     * */
    public String receidWhere(String receids)
    {
        String receid = receids.replace("a", "");
        String arr[]  = receid.split(",");
        String aids   = "",dids = "",gids="",val,lx,did1;
        for(int i=0;i<arr.length;i++) {
            val = arr[i];
            if(Rock.isEmpt(val) || val.equals("0"))continue;
            lx  = val.substring(0, 1);
            if(lx.equals("d")){
                did1 = this.getDeptdown(val.substring(1));
                if(!Rock.isEmpt(dids))dids+=",";
                dids+= did1;
            }else if(lx.equals("g")){

            }else{
                if(!Rock.isEmpt(aids))aids+=",";
                aids+= val;
            }
        }
        String where = "";
        if(!Rock.isEmpt(aids))where = "`id` IN("+aids+")";
        if(!Rock.isEmpt(dids)) {
            if(!Rock.isEmpt(where))where+=" OR ";
            where+= "`deptid` IN("+dids+")";
        }
        if(!Rock.isEmpt(where))where = "("+where+")";
        return where;
    }

    /**
     * 是否在receid的里面
     * */
    public Boolean receidInaid(String receid, String aid)
    {
        if(Rock.isEmpt(receid))return false;
        String where = receidWhere(receid);
        int to = this.nDb.getCount(DB.chuliPerfix("usera"), "`id`="+aid+" AND "+where+"");
        return to>0;
    }
}
